﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using DataAccess;

namespace InsuranceVehicle
{
    public partial class Claim : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            panelPolicyInfo.Visible = false;
            if (!IsPostBack)
            {
                //Calendar1.Visible = false;
                panelError.Visible = false;
                if (Session["CustomerID"] == null)
                {
                    panelError.Visible = true;
                    panelClaim.Visible = false;
                    lblErrLogin.Text = "You must login to Claim";
                }
                else
                {
                    CustomerInfo c = (CustomerInfo)Session["Customer"];
                    txtFirstname.Text = c.Firstname;
                    txtLastname.Text = c.Lastname;
                    txtPersonal.Text = c.PersonalIdentity;

                    addComboboxItem();
                }
            }

        }

        public void addComboboxItem()
        {
            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            int custID = Convert.ToInt32(Session["CustomerID"].ToString());
            List<InsurancePolicyInfo> list = ipa.getListOfPolicyByCustID(custID);
            foreach (var item in list)
            {
                cbbPolicyID.Items.Add(item.PolicyID.ToString());
            }
        }

        protected void btnClaim_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                if (cbbPolicyID.SelectedIndex == 0)
                {
                    lblError.Visible = true;
                    lblError.Text = "Please, choose your policy";
                }
                else
                {
                    ClaimDataAccess cda = new ClaimDataAccess();
                    ClaimInfo c = new ClaimInfo();
                    c.PolicyID = Convert.ToInt32(cbbPolicyID.SelectedItem.ToString());
                    c.CustomerID = Convert.ToInt32(Session["CustomerID"].ToString());
                    c.ClaimDate = Convert.ToDateTime(DateTime.Today.ToShortDateString());
                    c.PlaceOfAccident = txtAccidentPlace.Text;
                    c.DateOfAccident = Convert.ToDateTime(RadDatePicker1.SelectedDate.Value.ToShortDateString());
                    c.DescriptionDamage = txtDesDamage.Text;

                    lblError.Visible = true;
                    lblError.Text = cda.InsertClaim(c);

                }
            }

        }

        public void setPolicyInfomation()
        {
            InsurancePolicyAccess ipa = new InsurancePolicyAccess();
            List<InsurancePolicyInfo> list = new List<InsurancePolicyInfo>();
            int policyID = Convert.ToInt32(cbbPolicyID.SelectedItem.ToString());
            list = ipa.getDetailPolicyByID(policyID);

            foreach (var item in list)
            {
                if (item.PolicyID == policyID)
                {
                    lblCustomername.Text = item.CustomerName;
                    lblInsuranceType.Text = item.InsuranceType;
                    lblVehicle.Text = item.PerVehicleControlPlate;
                    lblPolicyDate.Text = item.PolicyDate.ToShortDateString();
                }
            }
        }

        protected void cbbPolicyID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbPolicyID.SelectedIndex != 0)
            {
                panelPolicyInfo.Visible = true;
                setPolicyInfomation();
            }
            else
            {
                panelPolicyInfo.Visible = false;
            }
            
        }

    }
}